#include "liftcontrol.h"

LiftControl::LiftControl(QWidget *parent) : QWidget(parent)
{
}

LiftControl::LiftControl(const QString &Name, const QJsonObject &functionDetails, QWidget *parent)
    : QWidget(parent)
{
    QGroupBox *groupBox = new QGroupBox("举升缸控制", this);
    groupBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    createLiftControl(groupBox, functionDetails);

    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->addWidget(groupBox);
    layout->setContentsMargins(0, 0, 0, 0);
    qDebug() << "测试是否进入LiftControl";

    setLayout(layout);
}

void LiftControl::createLiftControl(QGroupBox *groupBox, const QJsonObject &functionDetails)
{
    qDebug() << "functionDetails :" << functionDetails;
    QVBoxLayout *mainLayout = new QVBoxLayout();
    groupBox->setLayout(mainLayout);

    // 创建用于放置控件的窗口
    QWidget *LabelWidget = new QWidget(groupBox);
    QHBoxLayout *LabelLayout = new QHBoxLayout(LabelWidget);
    LabelWidget->setLayout(LabelLayout);
    LabelWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    QJsonObject Details = functionDetails["Parameters"].toObject();
    QJsonObject labelDetails = Details["label"].toObject();
    qDebug() << "Label Details:" << labelDetails;

    QLabel *labelHeight = new QLabel("Height: ", LabelWidget);
    QLineEdit *LineEditHeight = new QLineEdit(LabelWidget);
    LineEditHeight->setText(labelDetails["height"].toString());
    QLabel *labelValveId = new QLabel("Valve ID: ", LabelWidget);

    LabelLayout->addWidget(labelHeight, 1);
    LabelLayout->addWidget(LineEditHeight, 1);
    LabelLayout->addWidget(labelValveId, 1);

    QComboBox *comboBox = new QComboBox(LabelWidget);
    QStringList Items = {"Option 1", "Option 2", "Option 3", "Option 4"};
    comboBox->addItems(Items);
    LabelLayout->addWidget(comboBox, 1);
    connect(comboBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
            this, &LiftControl::onComboBoxIndexChanged);

    // 创建用于放置按钮的窗口
    QWidget *PushButtonWidget = new QWidget(groupBox);
    QHBoxLayout *PushButtonLayout = new QHBoxLayout(PushButtonWidget);
    PushButtonWidget->setLayout(PushButtonLayout);
    PushButtonWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    QJsonArray pushbuttonArray = Details["pushbutton"].toArray();
    qDebug() << "Pushbutton Array:" << pushbuttonArray;

    // 遍历 pushbuttonArray，为每个按钮连接同一个槽函数
    for (int i = 0; i < pushbuttonArray.size(); ++i) {
        QString buttonText = pushbuttonArray[i].toString();
        QPushButton *button = new QPushButton(buttonText, PushButtonWidget);
        button->setMaximumSize(100, 60);
        PushButtonLayout->addWidget(button);

        connect(button, &QPushButton::clicked, this, &LiftControl::onButtonClicked);
    }

    mainLayout->addWidget(LabelWidget);
    mainLayout->addWidget(PushButtonWidget);
}

void LiftControl::onComboBoxIndexChanged(int index)
{
    qDebug() << "ComboBox selected index changed to:" << index;
    // 根据 index 执行对应操作
}

void LiftControl::onButtonClicked()
{
    QPushButton *button = qobject_cast<QPushButton*>(sender());
    if (!button)
        return;

    QString buttonText = button->text();
    qDebug() << "Button clicked:" << buttonText;

    // 根据按钮的文本执行不同的逻辑
    if (buttonText == "preLift") {
        qDebug() << "preLift logic executed";
        // 添加 preLift 的逻辑代码
    } else if (buttonText == "liftHome") {
        qDebug() << "liftHome logic executed";
        // 添加 liftHome 的逻辑代码
    } else if (buttonText == "Lift") {
        qDebug() << "Lift logic executed";
        // 添加 Lift 的逻辑代码
    } else if (buttonText == "StopLift") {
        qDebug() << "StopLift logic executed";
        // 添加 StopLift 的逻辑代码
    } else {
        qDebug() << "Unknown button clicked:" << buttonText;
    }
}
